//
//  AdView.m
//  SXH
//
//  Created by Ryan on 16/6/15.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "AdView.h"
#import "UIImageView+WebCache.h"

#define picHeight (428/375.0*ScreenWidth)
#define picWidth (289/375.0*ScreenWidth)

@implementation AdView

- (instancetype)initWithArray:(NSArray*)listArray
{
    self = [super init];
    if (self) {
        _AdArray = listArray;
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disMissView)];
        [self addGestureRecognizer:tap];
        int count = (int)[listArray count];
        if (!_scrollView) {
            self.scrollView = [[UIScrollView alloc]init];
            [self addSubview:self.scrollView];
            [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0);
                make.right.equalTo(@0);
                make.centerY.equalTo(self);
                make.height.equalTo(@(picHeight));
            }];
            self.scrollView.contentSize = CGSizeMake(ScreenWidth*count, 0);
            self.scrollView.pagingEnabled = YES;
            self.scrollView.showsHorizontalScrollIndicator = NO;
            self.scrollView.delegate = self;
        }
        self.pageCtrl = [[UIPageControl alloc] init];  //创建UIPageControl，位置在屏幕最下方。
        self.pageCtrl.numberOfPages = count;//总的图片页数
        self.pageCtrl.currentPage = 0; //当前页
        self.pageCtrl.currentPageIndicatorTintColor = [UIColor greenColor];
        self.pageCtrl.pageIndicatorTintColor = [UIColor grayColor];

        [self.pageCtrl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];  //用户点击UIPageControl的响应函数
        [self addSubview:self.pageCtrl];  //将UIPageControl添加到主界面上。
        [self.pageCtrl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@100);
            make.height.equalTo(@30);
            make.bottom.equalTo(self.scrollView.mas_bottom).offset(-10);
            make.centerX.equalTo(self);
        }];
        for (int i = 0; i < count ; i++) {
            ADModel *model = [listArray objectAtIndex:i];
            UIView *subView = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, picHeight)];
            [self.scrollView addSubview:subView];
            UIImageView *subImageView = [[UIImageView alloc]init];
            [subView addSubview:subImageView];
            [subImageView sd_setImageWithURL:[NSURL URLWithString:IMAGE_STRING(model.pic)]];
            [subImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(picWidth));
                make.height.equalTo(subView.mas_height);
                make.center.equalTo(subView);
            }];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAtPic:)];
            [subImageView addGestureRecognizer:tap];
            subImageView.userInteractionEnabled = YES;
            subImageView.tag = i;
        }

        
        UIImageView *closeBtn = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"btn_close"]];
        [self addSubview:closeBtn];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView.mas_bottom).offset(25);
            make.centerX.equalTo(self);
            make.height.equalTo(@30);
            make.width.equalTo(@30);
        }];
    }
    return self;
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //更新UIPageControl的当前页
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.frame;
    [self.pageCtrl setCurrentPage:offset.x / bounds.size.width];
    NSLog(@"%f",offset.x / bounds.size.width);
}

- (void)pageTurn:(UIPageControl*)sender
{
    //令UIScrollView做出相应的滑动显示
    CGSize viewSize = self.scrollView.frame.size;
    CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
    [self.scrollView scrollRectToVisible:rect animated:YES];
}

- (void)clickAtPic:(UITapGestureRecognizer*)tap
{
    if (_delegate) {
        [_delegate ClickAtModel:[self.AdArray objectAtIndex:tap.view.tag]];
    }
    [self disMissView];
}

- (void)show
{
    self.scrollView.alpha = 0;
    self.pageCtrl.alpha = 0;
    [UIView animateWithDuration:0.55 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        self.scrollView.alpha = 1;
        self.pageCtrl.alpha = 1;

    }];
}

- (void)disMissView
{
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        self.scrollView.alpha = 0;
        self.pageCtrl.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];

    }];
}

@end
